<template>
  <div class="header">
    <!-- 左列 -->
    <span class="logo">商城后台管理系统</span>
    <el-icon @click="setAsideWidthHandle"><Fold /></el-icon>
    <div class="f_right">
      <el-tooltip
        :enterable="false"
        effect="dark"
        placement="bottom"
        content="刷新"
      >
        <el-icon class="icon"><Refresh @click="refreshHandle" /></el-icon>
      </el-tooltip>
      <el-tooltip
        :enterable="false"
        effect="dark"
        placement="bottom"
        content="全屏"
      >
        <el-icon class="icon" @click="toggle"><FullScreen /></el-icon>
      </el-tooltip>

      <el-dropdown class="el-dropdown" @command="commandHandle">
        <span>
          <el-avatar
            class="el-avatar"
            :size="30"
            src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
          ></el-avatar>
          {{ $store.state.userInfo.username }}
          <el-icon class="icon"><ArrowDown /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="editPassword">修改密码</el-dropdown-item>
            <el-dropdown-item command="logoutFn">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <!-- 修改密码 -->
    <el-dialog v-model="dialogEditPassword" title="修改密码" width="40%">
      <el-form
        ref="ruleFormRefEdit"
        :model="ruleFormEdit"
        :rules="rulesEdit"
        label-width="80px"
      >
        <el-form-item label="原密码" prop="oldpassword">
          <el-input v-model="ruleFormEdit.oldpassword"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input v-model="ruleFormEdit.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="repassword">
          <el-input v-model="ruleFormEdit.repassword"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-foote">
          <el-button @click="dialogEditPassword = false">取消</el-button>
          <el-button type="primary" @click="editPasswordHandle">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { Fold, Refresh, FullScreen, ArrowDown } from "@element-plus/icons-vue";
import { useFullscreen } from "@vueuse/core";
// 浏览器全屏方法
const { toggle } = useFullscreen();
import { ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { useEditPassword } from "@/utils/useEditPassword";
// 结构修改密码的属性与方法
const {
  dialogEditPassword,
  ruleFormEdit,
  rulesEdit,
  ruleFormRefEdit,
  editPasswordHandle,
} = useEditPassword();
const store = useStore();
const router = useRouter();
const commandHandle = (res) => {
  if (res == "editPassword") {
    // 修改密码
    dialogEditPassword.value = true;
  }
  if (res == "logoutFn") {
    // 退出登录
    logout()
  }
};
// 退出登录
const logout = async () => {
  const res = await ElMessageBox.confirm("是否退出登录", "注意", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).catch((err) => {
    return err;
  });
  if (res == "confirm") {
    // 删除本地token
    window.sessionStorage.removeItem("token");
    // 删除vuex中用户信息
    store.commit("setUserInfo", {});
    // 跳转至登录页
    router.push({ path: "/login" });
  }
};
// 刷新
const refreshHandle = () => {
  location.reload();
};
// 控制左侧菜单栏的显示与隐藏
const setAsideWidthHandle = () => {
    store.commit('setAsidewidth')
}
</script>

<style lang="less" scoped>
.header {
  height: 100%;
  display: flex;
  color: #fff;
  align-items: center;
  .f_right {
    margin-left: auto;
    display: flex;
    align-items: center;
    height: 100%;
    padding-right: 20px;
    .el-dropdown {
      margin-left: 20px;
      color: #fff;
      cursor: pointer;
      span {
        display: flex;
        align-items: center;
        .el-avatar {
          margin-right: 10px;
        }
      }
    }
  }
  .logo {
    font-size: 18px;
    padding-right: 16px;
  }
  .icon {
    width: 30px;
    height: 50%;
    cursor: pointer;
    font-size: 18px;
    font-weight: bold;
  }
  .icon:hover {
    background: #065327;
  }
}
</style>
